/**********************************************************************
*The 都file replicates all regression results in the manuscript  "Open Space in U.S. Urban Areas: Where Might There Be Too Much or Too Little of a Good Thing?", including the following three parts

1. 1970-2010   open space measure: administrative delineation: Table 3, Table E1 col(1) and (1') 
2. 1970-2010   open space measure: 50km radius circle: Table E2 col(1) and (1')  Table E3
3. 1980-2010   open space measure: administrative delineation: Table E1 col(2) and (2') 
4. 1980-2010   open space measure: 50km radius circle: Table E2 col(2) and (2'),  Table E3

************************************************************************/
clear all
cd "D:dataset and code"
use final70-10,clear

****generate interaction term
gen int_lambda = lambda*dlgtotunit
gen int_lambdapop = lambda*lgpop_base*dlgtotunit
gen int_wri = dlgtotunit*lgwri
gen int_pop = dlgtotunit*lgpop_base   
gen iv1 = lgjansunhrs * prot_iv
gen iv2 = lgjansunhrs * ntrad_iv
gen iv3 = immshock * prot_iv
gen iv4 = immshock * ntrad_iv
gen iv5 = bartik_iv * prot_iv
gen iv6 = bartik_iv * ntrad_iv



/***************************************************************************
              1. 1970-2010   open space measure: administrative delineation:
			  Table 3, Table E1 col(1) and (1') 
******************************************************************************/


*******************  *Table 3***************************************************
****model (A)  **********
global sincl_exog "midwest south west"
global endogvar "dlgtotunit    int_pop   "
global excl_exog  " prot_iv   ntrad_iv     lgpermit lgconstr_wage    lgrsmcost_base"
global incl_exog  "dlgmedfminc dlgcommute lgjansunhrs coast  immshock bartik_emp    lambda  midwest south west" 
ivreg2 dlgmedval  midwest south west  (int_lambda int_lambdapop int_wri = bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6 ), gmm2s r first ffirst savefirst savefprefix(firsts_) //supply equation
ivreg2 dlgmedval $incl_exog  ($endogvar= $excl_exog) , gmm2s r first ffirst savefirst savefprefix(firsts_)	//demand equation


****model (B)  **********
global sincl_exog "midwest south west"
global endogvar "dlgtotunit    int_pop   "
global excl_exog  "  prot_iv    ntrad_iv   lgpermit     lgconstr_wage" 
global excl_exog  " prot_iv   ntrad_iv     lgpermit lgconstr_wage    lgrsmcost_base"
global incl_exog  "dlgmedfminc dlgcommute lgjansunhrs coast  immshock bartik_emp    lambda  midwest south west" 
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust) 


****model (C) **********
set more off
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop  opensp1"
global excl_exog  "dprot_iv       ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10   dlgsm1ls15_m3f  dlgsm1outlin"
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock lambda coast midwest south west"
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


****model (D) ********** 
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop   opensp1 opensp2"
global excl_exog  "dprot_iv          ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10  dlgsm1ls15_m3f dlgsm1outlin   dlgsm2ls15_m3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


****model (E)  ********** 
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop  opensp1 opensp2 "
global excl_exog  "dprot_iv   prot_iv   ntrad_iv     lgpermit   dlgconstr_wage  dlgrsmcost    oscomm00 oscomm10  dlgsm1ls15_m3f  dlgsm1outlin dlgsm2ls15_m3f  dlgsm2outlin"
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock lambda coast midwest south west"
gmm (eq1: dlgmedval - {xb:  int_lambda int_lambdapop int_wri opensp1 opensp2  $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  oscomm10 dlgsm1ls15_m3f  dlgsm1outlin dlgsm2ls15_m3f  dlgsm2outlin $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


*******************Table E1*************************************************
****column 1 **********
set more off
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop  opensp1"
global excl_exog  "dprot_iv       ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10   dlgsm1ls15_m3f  dlgsm1outlin"
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock lambda coast midwest south west"
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


***Column 1' ********** 
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop   opensp1 opensp2"
global excl_exog  "dprot_iv          ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10  dlgsm1ls15_m3f dlgsm1outlin   dlgsm2ls15_m3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  



*******************Table E4*************************************************

set more off
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop dshare1pub dshare1pvt dshare2pub dshare2pvt "
global excl_exog  "dprot_iv ntrad_iv dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10  dlgsm1ls15_m3f dlgsm1outlin   dlgsm2ls15_m3f dlgsm2outlin ease21comm fease21comm  ease38comm fease38comm    "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


/***************************************************************************
                   2.1970-2010: open space measure: 50km radius circle
				   Table E2 col(1) and (1')  Table E3
******************************************************************************/


replace lambda=lambda_mb
replace opensp1 = opensp1_50
replace opensp2 = opensp2_50
replace int_lambda = lambda*dlgtotunit
replace int_lambdapop = lambda*lgpop_base*dlgtotunit

*******************Table E2:different measures*************************************************

*****Column1 *********

global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop     opensp1"
global excl_exog  "dprot_iv      prot_iv     ntrad_iv   dlgpermit      dlgconstr_wage  dlgrsmcost     oscommb10 oscommb00  dlgsm2ls15_mb3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "

gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  

*****Column1'********* 
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop   opensp1 opensp2"
global excl_exog  "dprot_iv          ntrad_iv         dlgconstr_wage  dlgrsmcost   oscommb00 oscommb10  dlgsm1ls15_mb3f dlgsm1outlin   dlgsm2ls15_mb3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


*******************Table E3*************************************************
//model (a)
global sincl_exog "midwest south west"
global endogvar "dlgtotunit    int_pop   "
global excl_exog  "  prot_iv    ntrad_iv   lgpermit     lgconstr_wage" 
global incl_exog  "dlgmedfminc dlgcommute lgjansunhrs coast  immshock bartik_emp    lambda  midwest south west" 
ivreg2 dlgmedval  midwest south west  (int_lambda int_lambdapop int_wri = bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6 ), gmm2s r first ffirst savefirst savefprefix(firsts_)  //supply equation
ivreg2 dlgmedval $incl_exog  ($endogvar= $excl_exog) , gmm2s r first ffirst savefirst savefprefix(firsts_)      //demand equation

//model (b)
global sincl_exog "midwest south west"
global endogvar "dlgtotunit    int_pop   "
global excl_exog  "  prot_iv    ntrad_iv   lgpermit     lgconstr_wage" 
global incl_exog  "dlgmedfminc dlgcommute lgjansunhrs coast  immshock bartik_emp    lambda  midwest south west" 
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust) 

//model (c)
global sincl_exog "midwest south west"
global endogvar "  dlgtotunit        int_pop         dlgpop   opensp1 dlgmunipc"
global excl_exog  "  prot_iv  ntrad_iv  lgpermit   lgconstr_wage   agecomm1980  wagecomm1980  oscommb10       dlgsmoutline     municomm1980  "
global incl_exog "dlgmedfminc dlgcommute bartik_emp lgjansunhrs   immshock     lambda coast midwest south west"
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust) 

//model (d)
global sincl_exog "midwest south west"
global endogvar "  dlgtotunit    int_lambdapop   dlgpop   opensp1 "
global excl_exog  "  prot_iv  ntrad_iv lgpermit   lgconstr_wage   agecomm1980  wagecomm1980  oscommb10         dlgsmoutline                 "  
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs   immshock    coast midwest south west"
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments( eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  

//model (e)

global sincl_exog "midwest south west"
global endogvar "  dlgtotunit         int_lambdapop   dlgpop   opensp1 "
global excl_exog  "  prot_iv ntrad_iv lgpermit   lgconstr_wage   agecomm1980  wagecomm1980  oscommb10         dlgsmoutline     " 
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs   immshock    coast midwest south west"
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri opensp1 $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog oscommb10         dlgsmoutline,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  






/***************************************************************************
            3.  1980-2010: open space measure: administrative delineation
			 Table E1 col(2) and (2') 
***************************************************************************/
use final80-10,clear
****generate interaction term

gen int_lambda = lambda*dlgtotunit
gen int_lambdapop = lambda*lgpop_base*dlgtotunit
gen int_wri = dlgtotunit*lgwri
gen int_pop = dlgtotunit*lgpop_base   
gen iv1 = lgjansunhrs * prot_iv
gen iv2 = lgjansunhrs * ntrad_iv
gen iv3 = immshock * prot_iv
gen iv4 = immshock * ntrad_iv
gen iv5 = bartik_iv * prot_iv
gen iv6 = bartik_iv * ntrad_iv
*******************Table E1*************************************************
****column 2 **********
set more off
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop  opensp1"
global excl_exog  "dprot_iv       ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10   dlgsm1ls15_m3f  dlgsm1outlin"
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock lambda coast midwest south west"
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  


***Column 2' ********** 
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop   opensp1 opensp2"
global excl_exog  "dprot_iv          ntrad_iv         dlgconstr_wage  dlgrsmcost   oscomm00 oscomm10  dlgsm1ls15_m3f dlgsm1outlin   dlgsm2ls15_m3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  



/***************************************************************************
                   4. 1980-2010: open space measure: 50km radius circle
				    Table E2 col(2) and (2'),  Table E3
***************************************************************************/

replace lambda=lambda_mb
replace opensp1 = dlgsm1ls15_mb2
replace opensp2 = dlgsm2ls15_mb2
replace int_lambda = lambda*dlgtotunit
replace int_lambdapop = lambda*lgpop_base*dlgtotunit

*******************Table E2:different measures**************************\

*****Column2 *********

global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop     opensp1"
global excl_exog  "dprot_iv      prot_iv     ntrad_iv   dlgpermit      dlgconstr_wage  dlgrsmcost     oscommb10 oscommb00  dlgsm2ls15_mb3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "

gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  

*****Column2'********* 
global sincl_exog "midwest south west"
global endogvar "dlgtotunit  int_pop   int_lambdapop   opensp1 opensp2"
global excl_exog  "dprot_iv          ntrad_iv         dlgconstr_wage  dlgrsmcost   oscommb00 oscommb10  dlgsm1ls15_mb3f dlgsm1outlin   dlgsm2ls15_mb3f dlgsm2outlin "
global incl_exog "dlgmunipc dlgmedfminc dlgcommute bartik_emp lgjansunhrs immshock  lambda coast midwest south west "
gmm (eq1: dlgmedval - {xb: int_lambda int_lambdapop int_wri $sincl_exog } - {b0} )  (eq2: dlgmedval - {xc: $endogvar  $incl_exog  } - {c0} ), instruments(eq1: bartik_iv lgjansunhrs immshock prot_iv ntrad_iv iv1-iv6  $sincl_exog ,nocons)  instruments(eq2: $excl_exog $incl_exog ,nocons ) winitial(identity) wmatrix(robust)  









